package com.vmc.model;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import com.vmc.util.TVDestinationPathLocator;

public class TVTransferDetail extends AbstractTransferDetail {
	private String season;
	private String episode;

	private static final String EPISODE_FORMAT = "Episode %s-%s";
	
	public TVTransferDetail() {
		super(null, null, null, null, DetailType.TV);
	}
	
	public TVTransferDetail(String season, String episode, String title,
			String extension, String location, String sourceFilename) {
		super(title, extension, location, sourceFilename, DetailType.TV);
		this.season = season;
		this.episode = episode;
	}
	
	public String getSeason() {
		return season;
	}
	public void setSeason(String season) {
		this.season = season;
	}
	public String getEpisode() {
		return episode;
	}
	public void setEpisode(String episode) {
		this.episode = episode;
	}

	@Override
	public String getDestinationFilePath() {
		return String.format(FORMAT, this.getLocation(), String.format(EPISODE_FORMAT, this.getSeason(), this.getEpisode()), this.getExtension());
	}

	public static class Builder {
		private static final Pattern seasonEpisodePattern = 
			Pattern.compile("([^a-zA-Z0-9])([Ss]+\\d{2}(\\s*)([-_]*)[Ee]+\\d{2})([^a-zA-Z0-9])");
		private static final Pattern numericPattern = Pattern.compile("\\d{2}");
		
		public static TVTransferDetail build(File file, DownloadTransferProperties properties) {
			TVTransferDetail detail = null;
			
			if (file != null && properties != null) {
				String filename = file.getName();
				
				detail = new TVTransferDetail();
				Matcher seasonEpisodeMatcher = seasonEpisodePattern.matcher(filename);
				if (seasonEpisodeMatcher != null && seasonEpisodeMatcher.find()) {
					String seasonEpisode = removeUnwantedCharacters(seasonEpisodeMatcher.group());
					Matcher numericMatcher = numericPattern.matcher(seasonEpisode);
					String[] seasonEpisodeArray = new String[2];
					int counter = 0;
					while (numericMatcher.find()) {
						seasonEpisodeArray[counter++] = numericMatcher.group();
					}
					String title = filename.substring(0, seasonEpisodeMatcher.start() - 1);
					if (title != null) {
						title = removeUnwantedCharacters(title);
					}
					
					detail.setTitle(title);
					detail.setSeason(seasonEpisodeArray[0]);
					detail.setEpisode(seasonEpisodeArray[1]);
					detail.setExtension(FilenameUtils.getExtension(filename));
					detail.setSourceFilename(file.getAbsolutePath());
					
					File location = TVDestinationPathLocator.getInstance().getLocation(properties.getTvDestinationDirectory(), detail, !properties.isTvTransientUsed());

					if (location == null) {
						location = properties.getTvTransientDirectory();
					}
					
					if (location != null && location.isDirectory()) {
						detail.setLocation(location.getAbsolutePath());
					}
				}
				
			}
			
			return detail;
		}
		
		private static String removeUnwantedCharacters(String data) {
			if (data != null) {
				data = data
				.replaceAll("\\.", " ")
				.replaceAll("\\+", " ")
				.replaceAll("-", " ")
				.replaceAll("_", " ")
				.trim();
			}
			return data;
		}
	}
}
